/*
 * @Author: LiZn
 * @Date: 2021-08-20 00:27:15
 * @LastEditTime: 2021-08-22 23:03:30
 * @LastEditors: LiZn
 * @Description: file content
 * @FilePath: \todaynews\vue.config.js
 */
const path = require("path");
const bodyParser = require("body-parser")
const resolve = dir=> path.resolve(__dirname,dir);
module.exports = {
  configureWebpack:{
    name:"今日头条"
  },
  chainWebpack:config=>{
    config.module.rule('svg')
    .exclude.add(resolve('./src/assets/icons'))
    //新增验证
    config.module.rule('icons')
    .test(/\.svg$/)
    .include.add(resolve('./src/assets/icons')).end()
    .use('svg-sprite-loader')
    .loader("svg-sprite-loader")
    .options({symbolId:'icon-[name]'})
  },
  publicPath:'/toutiao',
  devServer:{
    before:app=>{
      app.use(bodyParser.json()),
      app.post('/news/user/usersinfo',(req,res)=>{
        //根据用户的token获取用户的信息
        if(req.body.token=='qwerrewq'){
          res.json({
            code:2,
            data:{
              username:"lizn",
              roles:"admin",
            }
          })
        }else{
          res.json({
            code:3,
            data:{
              username:"",
              roles:"",
            }
          })
        }
      });
      //用户注册接口
      app.post('/news/user/login',(req,res)=>{
        const {username,password} = req.body;
        //做判断
        if(username=='lizn'&&password=='lx1993225'){
          res.json({
            code:2,
            data:{
              roles:'admin',
              token:'qwerrewq'
            },
            message:'用户设置成功'
          })
        }else{
          res.json({
            code:3,
            message:'您输入的密码或者用户名有误',
            data:{}
          })
        }
      })
      //用户点击关注接口
      app.post('/news/user/focus',(req,res)=>{
        const auth = req.headers['authorization']?req.headers['authorization'].split(' ')[0]:'';
        const {userId} = req.body;
        if(auth){
          res.json({
            code:2,
            data:{
              focusId:userId,//关注的用户
              ok:true
            }
          })
        }else{
          res.json({
            code:203,
            data:{}
          })
        }
      })
    }
  }
}